package com.hhxy.controller;

import com.hhxy.entity.Item;
import com.hhxy.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
@Controller
@RequestMapping("/item")
public class ItemConroller {

    @Autowired
    private ItemService itemService;

    /*  商品列表 */
    @RequestMapping("/queryAllItem")
    public String queryAllItem(Model model){

        List<Item> items = itemService.qeuryAllItem();
        model.addAttribute("items", items);
        return "itemList";
    }

    /* 跳转添加页面 */
    @RequestMapping("/toAddItem")
    public String toAddItem(){
        return "addItem";
    }

    /* 添加商品 */
    @RequestMapping("/addItem")
    public String addItem(Item item){
        itemService.saveItem(item);
        return "redirect:/item/queryAllItem.action";
    }

    /* 跳转修改页面 */
    @RequestMapping("/tooUpdate")
    public String tooUpdate(Integer id,Model model){
        Item item = itemService.qeuryById(id);
        model.addAttribute("item", item);
        return "updaItem";
    }

    /* 修改 */
    @RequestMapping("/updateItem")
    public String updateItem(Item item){
        itemService.updateItem(item);
        return "redirect:/item/queryAllItem.action";
    }

    /* 删除 */
    @RequestMapping("/deleteItem")
    public String deleteItem(Integer id){
        itemService.deleteItem(id);
        return "redirect:/item/queryAllItem.action";
    }
}